//
//  HXCropMessageBackgroundView.m
//  KD
//
//  Created by ifly on 16/7/18.
//  Copyright © 2016年 Whx. All rights reserved.
//

#define kCollectionViewJianJuLeft 40

#import "HXCropMessageBackgroundView.h"

#import "HXCMBackgroundVCollectionViewCell.h"
#import "HXChickUpHistoryCollectionViewCell.h"

@interface HXCropMessageBackgroundView ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UIScrollViewDelegate>



/**
 UICollectionView
 */
@property (nonatomic,strong) UICollectionView * collectionView;

@end

static NSString * identifierCell = @"HXCMBackgroundVCollectionViewCell";
static NSString * chickHistoryCell = @"HXChickUpHistoryCollectionViewCell";

@implementation HXCropMessageBackgroundView


-(void)setDataArray:(NSMutableArray<HXGetProjectMemberDetailModel *> *)dataArray{

    _dataArray = dataArray;
    
    [self.collectionView reloadData];
}

-(void)setChickUpHistoryArr:(NSMutableArray<HXGetChickUpHistoryDetailModel *> *)chickUpHistoryArr {
    
    _chickUpHistoryArr = chickUpHistoryArr;
    [self.collectionView reloadData];
}



/**显示*/
-(void)showView{

    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    self.center = window.center;
    [window addSubview:self];
}

-(instancetype)initWithFrame:(CGRect)frame
             withHistoryType:(HistoryType)type{

    if ([super initWithFrame:frame]) {
        
        self.backgroundColor = HXCustomColor(184, 184, 184, 0.8);
        self.type = type;
        //创建UICollectionView
        [self loadCreateUICollectionView];
        
        //创建btn
        UIButton * canleBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-36-8, 20+8, 36, 36)];
        //        [canleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [canleBtn setImage:[UIImage imageNamed:@"x"] forState:UIControlStateNormal];
        [canleBtn addTarget:self action:@selector(canleBtnAct) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:canleBtn];
        
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAct:)];
        tapGesture.numberOfTapsRequired = 1;
        tapGesture.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:tapGesture];
        
    }
    
    return self;

}


-(instancetype)initWithFrame:(CGRect)frame{

    if ([super initWithFrame:frame]) {
        
        self.backgroundColor = HXCustomColor(184, 184, 184, 0.8);
        
        //创建UICollectionView
        [self loadCreateUICollectionView];
        
        //创建btn
        UIButton * canleBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-36-8, 20+8, 36, 36)];
        //        [canleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [canleBtn setImage:[UIImage imageNamed:@"x"] forState:UIControlStateNormal];
        [canleBtn addTarget:self action:@selector(canleBtnAct) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:canleBtn];
        
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAct:)];
        tapGesture.numberOfTapsRequired = 1;
        tapGesture.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:tapGesture];

    }
    
    return self;
}

/**
 重写init方法 1.需要corpType 2.prjNum 3. keyGuid
 */
//-(instancetype)initWithFrame:(CGRect)frame
//                withCorpType:(NSString *)corpType
//                  withPrjNum:(NSString *)prjNum
//                 withKeyGuid:(NSString *)keyGuid{
//
//    if ([super initWithFrame:frame]) {
//        
//        self.corpType = corpType;
//        self.prjNum   = prjNum;
//        self.keyGuid  = keyGuid;
//        
////        [self getDate];
//        self.backgroundColor = HXCustomColor(184, 184, 184, 0.8);
//        
//        //创建UICollectionView
//        [self loadCreateUICollectionView];
//        
//        //创建btn
//        UIButton * canleBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-36-8, 20+8, 36, 36)];
////        [canleBtn setTitle:@"取消" forState:UIControlStateNormal];
//        [canleBtn setImage:[UIImage imageNamed:@"x"] forState:UIControlStateNormal];
//        [canleBtn addTarget:self action:@selector(canleBtnAct) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:canleBtn];
//        
//        
//        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAct:)];
//        tapGesture.numberOfTapsRequired = 1;
//        tapGesture.numberOfTouchesRequired = 1;
//        [self addGestureRecognizer:tapGesture];
//        
//    }
//    
//    return self;
//}

/**
 btn的点击事件
 */
-(void)canleBtnAct{

    [self removeFromSuperview];
}

/**
 手势点击方法
 */
-(void)tapGestureAct:(UITapGestureRecognizer *)tap{

    if (tap.numberOfTapsRequired == 1) {
        
        [self removeFromSuperview];
    }
}


/**
 Layout
 */
-(UICollectionViewFlowLayout *)loadCreateUICollectionViewLayout{

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumLineSpacing      = 0;
    layout.minimumInteritemSpacing = 0;
    
    layout.scrollDirection         = UICollectionViewScrollDirectionHorizontal;
    
    return layout;
}

/**
 创建UICollectionView
 */
-(void)loadCreateUICollectionView{

    if (_collectionView == nil) {
        
        _collectionView            = [[UICollectionView alloc] initWithFrame:CGRectMake(kCollectionViewJianJuLeft, (HXSCREEN_HEIGHT-(self.width - kCollectionViewJianJuLeft*2)*HXSCREEN_HEIGHT/HXSCREEN_WIDTH)/2, self.width - kCollectionViewJianJuLeft * 2, (self.width - kCollectionViewJianJuLeft*2)*HXSCREEN_HEIGHT/HXSCREEN_WIDTH)
                                             collectionViewLayout:[self loadCreateUICollectionViewLayout]];
        
        _collectionView.delegate   = self;
        _collectionView.dataSource = self;
        
        [self addSubview:_collectionView];
        
        _collectionView.backgroundColor = [UIColor clearColor];
        
        //注册单元格
        if (self.type == ProjectHistoryType) {
            [_collectionView registerNib:[UINib nibWithNibName:@"HXCMBackgroundVCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifierCell];
        }
        else if (self.type == ChickUpHistoryType) {
            [_collectionView registerNib:[UINib nibWithNibName:@"HXChickUpHistoryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:chickHistoryCell];
        }
    }
}

#pragma mark ------// UICollectionViewDataSource \\----------
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    if (self.type == ProjectHistoryType) {
        return self.dataArray.count;
    }
    else if (self.type == ChickUpHistoryType) {
        return self.chickUpHistoryArr.count;
    }
    else {
        return 0;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == ProjectHistoryType) {
        HXCMBackgroundVCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell forIndexPath:indexPath];
        cell.transform = CGAffineTransformMakeScale(0.98, 0.98);
        cell.data = _dataArray[indexPath.row];
        
        return cell;
    }
    else if (self.type == ChickUpHistoryType) {
        HXChickUpHistoryCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:chickHistoryCell forIndexPath:indexPath];
        cell.transform = CGAffineTransformMakeScale(0.98, 0.98);
        cell.data = _chickUpHistoryArr[indexPath.row];
        return cell;

    }
    return nil;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(_collectionView.width, _collectionView.height);
}

//-(NSMutableArray<HXGetProjectMemberDetailModel *> *)dataArray {
//    if (_dataArray == nil) {
//        _dataArray = [[NSMutableArray alloc] init];
//    }
//    return _dataArray;
//}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    
    //获取偏移量
    NSInteger index = (targetContentOffset->x + _collectionView.width/2)/_collectionView.width;
    
    targetContentOffset->x = index * _collectionView.width;
}



@end
